﻿using System.Globalization;
using System.Web.Security;
using BankApp.Models;
using IBS.Bll;
using IBS.Model;
using IBS.ServiceContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Client = CommonApp.Models.Client;

namespace BankApp.Controllers
{
    [Authorize(Roles = "operator")]
    public class ClientsController : Controller
    {
        private IKnowledgeBaseService knowledgeBase;
        private ICardService cardService;

        public ClientsController()
        {
            var container = new IbsContainer();
            this.knowledgeBase = container.KnowledgeBaseService;
            this.cardService = container.CardService;
        }

        //
        // GET: /Clients/

        public ActionResult Index(string firstName = null, string lastName = null)
        {
            var allClientsResult = knowledgeBase.GetClients();

            var clients = allClientsResult
                .Where(c => (String.IsNullOrWhiteSpace(firstName) || c.FirstName.ToUpper() == firstName.ToUpper())
                    && (String.IsNullOrWhiteSpace(lastName) || c.LastName.ToUpper() == lastName.ToUpper()))
                .ToList();

            ViewBag.FirstName = firstName;
            ViewBag.LastName = lastName;

            return View(clients);
        }

        //
        // GET: /Clients/Client/5

        public ActionResult Client(int id)
        {
            var clientResult = knowledgeBase.GetClientById(id);
            var model = AutoMapper.Mapper.Map<Client>(clientResult);
            return View(model);
        }

        //
        // GET: /Clients/Create

        public ActionResult NewClient()
        {
            return View("Client", new Client());
        } 

        //
        // POST: /Clients/Create

        [HttpPost]
        public ActionResult NewClient(Client client)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var clientResult = AutoMapper.Mapper.Map<IBS.Model.Client>(client);
                    //var clientResult = knowledgeBase.GetClientById(client.Id);
                    //clientResult.FirstName = client.FirstName;
                    //clientResult.LastName = client.LastName;
                    //clientResult.PassportNumber = client.PassportNumber;
                    //clientResult.PassportSeries = client.PassportSeries;
                    //clientResult.DateOfBirth = client.DateOfBirth;

                    knowledgeBase.CreateClient(clientResult, 2, 2, User.Identity.Name);
                    return RedirectToAction("Index");
                }
                
            }
            catch (BusinessException be)
            {
                ModelState.AddModelError("", be.Message);
            }
            return View("Client", client);
        }         

        //
        // POST: /Clients/Edit/5

        [HttpPost]
        public ActionResult Edit(Client client)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var clientResult = knowledgeBase.GetClientById(client.Id.Value);
                    clientResult.FirstName = client.FirstName;
                    clientResult.LastName = client.LastName;
                    clientResult.PassportNumber = client.PassportNumber;
                    clientResult.PassportSeries = client.PassportSeries;
                    clientResult.DateOfBirth = client.DateOfBirth.Value;

                    knowledgeBase.UpdateClient(clientResult, User.Identity.Name);
                    return RedirectToAction("Index", "Clients");
                }
            }
            catch (BusinessException be)
            {
                ModelState.AddModelError("", be.Message);
            }
            return View("Client", client);
        }

        //
        // POST: /Clients/Delete/5

        [HttpPost]
        public ActionResult Delete(Client client)
        {
            try
            {
                if (cardService.GetClientsCard(knowledgeBase.GetClientById(client.Id.Value)).Any())
                {
                    ModelState.AddModelError("", "Cannot delete a client with cards");
                }
                else
                {
                    knowledgeBase.DeleteClient(client.Id.Value);
                    return RedirectToAction("Index");
                }
            }
            catch (BusinessException be)
            {
                ModelState.AddModelError("", be.Message);
            }
            return View("Client", client);
        }
 
        public ActionResult NewCard(int id)
        {
            return View(new CardModel { ClientId = id, StartAmount = 0});
        }

        [HttpPost]
        public ActionResult CreateCard(int ClientId, CardType CardType, decimal StartAmount = 0)
        {
            try
            {
                var client = knowledgeBase.GetClientById(ClientId);
                cardService.CreateNewCard(client, CardType, User.Identity.Name, StartAmount);
            }
            catch (BusinessException ex)
            {
                ModelState.AddModelError("", ex.Message);
            }
            return RedirectToAction("Index");
        }
    }
}
